﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data.Common;
using System.Data.OleDb;

/// <summary>
///DbDealWeibo 的摘要说明
/// </summary>
public class DbDealedWeibo
{
	public DbDealedWeibo()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}

    /// <summary>
    /// 添加已处理记录
    /// </summary>
    /// <param name="weiboId">微博ID</param>
    /// <param name="status">处理状态</param>
    /// <param name="dealTime">处理时间</param>
    /// <returns></returns>
    public bool Add(string weiboId, int status, DateTime dealTime)
    {
        DbCommand command = DBConfig.CreateCommand("Insert Into [dealedWeibo](weiboId, status, dealTime) Values(@weiboId, @status, @dealTime)");
        command.Parameters.AddRange(
            new DbParameter[]
            {
                DBConfig.CreateParameter("@weiboId", weiboId),
                DBConfig.CreateParameter("@status", status),
                DBConfig.CreateParameter("@dealTime", "#" + dealTime + "#", OleDbType.DBTimeStamp)
            }
            );
        using (DbConnection conn = DBConfig.CreateConnect())
        {
            command.Connection = conn;
            conn.Open();
            return command.ExecuteNonQuery() > 0;
        }
    }

    public bool Update(int id, int status, DateTime dealTime)
    {
        DbCommand command = DBConfig.CreateCommand("Update [dealedWeibo] set status=@status, dealTime=@dealTime");
        command.Parameters.AddRange(
            new DbParameter[]
            {
                DBConfig.CreateParameter("@status", status),
                DBConfig.CreateParameter("@dealTime", "#" + dealTime + "#", OleDbType.DBTimeStamp)
            }
            );
        using (DbConnection conn = DBConfig.CreateConnect())
        {
            command.Connection = conn;
            conn.Open();
            return command.ExecuteNonQuery() > 0;
        }
    }

    public bool DeleteList(DateTime lastTime)
    {
        DbCommand command = DBConfig.CreateCommand("Delete From [dealedWeibo] Where dealTime < @lastTime");
        command.Parameters.Add(DBConfig.CreateParameter("@lastTime", "#" + lastTime + "#", OleDbType.DBTimeStamp));
        using (DbConnection conn = DBConfig.CreateConnect())
        {
            command.Connection = conn;
            conn.Open();
            return command.ExecuteNonQuery() > 0;
        }
    }

    public DealedWeiboData Select(string weiboId)
    {
        DbCommand command = DBConfig.CreateCommand("Select * From [dealedWeibo] Where weiboId=@weiboId");
        command.Parameters.Add(DBConfig.CreateParameter("@weiboId", weiboId));
        using (DbConnection conn = DBConfig.CreateConnect())
        {
            command.Connection = conn;
            conn.Open();
            DbDataReader reader = command.ExecuteReader();
            DealedWeiboData result = null;
            if (reader.Read())
            {
                result = new DealedWeiboData();
                result.id = reader.GetInt32(0);
                result.weiboId = reader.GetString(1);
                result.status = reader.GetInt32(2);
                result.dealTime = reader.GetDateTime(3);
            }
            return result;

        }
    }

    public List<DealedWeiboData> SelectList()
    {
        DbCommand command = DBConfig.CreateCommand("Select * From [dealedWeibo]");
        using (DbConnection conn = DBConfig.CreateConnect())
        {
            command.Connection = conn;
            conn.Open();
            DbDataReader reader = command.ExecuteReader();
            List<DealedWeiboData> result = null;
            while (reader.Read())
            {
                if (result == null)
                    result = new List<DealedWeiboData>();
                DealedWeiboData data = new DealedWeiboData();
                data.id = reader.GetInt32(0);
                data.weiboId = reader.GetString(1);
                data.status = reader.GetInt32(2);
                data.dealTime = reader.GetDateTime(3);
                result.Add(data);
            }
            return result;
        }
    }
}